package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.templates.item.ItemIdentifyManage;

/**
 *  鉴定， 铭刻协议
 * @author zjj
 *
 */
public final class RequestIdentifyItem extends L2GameClientPacket {

	private static final String RequestIdentifyItem = "[C] 0x7b RequestIdentifyItem";
	
	private static final Logger _log = LoggerFactory.getLogger(RequestIdentifyItem.class.getName());

	private static final int OP_IDENTIFY = 0xff; //鉴定    //现已不需要！！！！！  正确的是  0  现在是非正确的 

	private static final int OP_INSCRIBE = 1; //铭刻

	private long _id; //物品uid 

	private int _op; //打造方式

	@Override
	public String getType() {
		return RequestIdentifyItem;
	}

	@Override
	protected void readImpl() {
		_id = readLong();
		_op = readByte();

	}

	@Override
	protected void runImpl() {
		FuzePcInstance player = getClient().getActiveChar();
		if (player == null) {
			return;
		}
		//物品不存在
		L2ItemInstance item = getClient().getActiveChar().getInventory().getItemByObjectId(_id);
		if (item == null) {
			_log.warn(player + "请求 物品不存在 id:" + _id, Config.SERVER_ID);
			return;
		}

		if(_log.isDebugEnabled()){
			_log.debug("{} 鉴定开始",player);
		}

		switch (_op) {
		case OP_IDENTIFY:
			if( !item.getItem().getCanIdentify() ){
				return;
			}
			if( item.isIdentify() )
			{
				//发送系统消息 装备已鉴定
				sendPacket(new SystemMessage(SystemMessageId.ITEM_HAS_IDENTIFY));
				return;
			}
			if (!ItemIdentifyManage.getInstance().identify(player, item, _op)) {
				//TODO:
				//通知客户端， 鉴定失败
				if(_log.isDebugEnabled()){
					_log.debug("{} 鉴定失败",player);
				}
				sendPacket(new SystemMessage(SystemMessageId.IDENTIFY_FAILED));
			} else {
				item.setIdentify(true);
			}
			break;
		case OP_INSCRIBE:
			if( !item.getItem().getCanInscribe() ){
				return;
			}
//			//如果装备还没有坚定  不能铭刻
//			if( !item.isIdentify() ){
//				
//				sendPacket(new SystemMessage(SystemMessageId.ITEM_CANOT_INSCRIBE));
//				return;
//				
//			}
			if( item.isInscribe() )
			{
				//发送系统消息 装备已鉴定
				sendPacket(new SystemMessage(SystemMessageId.ITEM_HAS_INSCRIBE));
				return;
			}
			if (!ItemIdentifyManage.getInstance().identify(player, item, _op)) {
				//TODO:
				//通知客户端， 铭刻失败
				if(_log.isDebugEnabled()){
					_log.debug("{} 铭刻失败",player);
				}
				sendPacket(new SystemMessage(SystemMessageId.INSRIBE_FAILED));
			} else {
				item.setInscribe(true);
			}
			break;
		}

	}// end 

}
